package com.example.androidlearning2200;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;

import java.util.Random;

public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        findViewById(R.id.btnSendHigh).setOnClickListener(view -> sendNotificationHigh());
        findViewById(R.id.btnSendDefault).setOnClickListener(view -> sendNotificationDefault());
    }

    /**
     * 发送重要通知的方法
     */
    private void sendNotificationHigh() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 通知渠道，给通知的重要性进行分类，方便用户有选择的接收通知
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(this.getLocalClassName() + 100, "重要通知", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(notificationChannel);
        }
        Intent intent = new Intent(this, ChatActivity1.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1000, intent, PendingIntent.FLAG_IMMUTABLE);
        Notification notification = new NotificationCompat.Builder(this, this.getLocalClassName() + 100)
                .setContentTitle("通知标题")
                .setContentText("通知内容通知内容通知内容通知内容通知内容")
                .setSmallIcon(android.R.mipmap.sym_def_app_icon)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.avatar))
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(new Random().nextInt(), notification);
    }

    /**
     * 发送普通通知的方法
     */
    private void sendNotificationDefault() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 通知渠道，给通知的重要性进行分类，方便用户有选择的接收通知
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(this.getLocalClassName() + 101, "普通通知", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(notificationChannel);
        }
        Notification notification = new NotificationCompat.Builder(this, this.getLocalClassName() + 101)
                .setContentTitle("通知标题")
                .setContentText("通知内容通知内容通知内容通知内容通知内容")
                .setSmallIcon(android.R.mipmap.sym_def_app_icon)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.avatar))
                .build();
        notificationManager.notify(new Random().nextInt(), notification);
    }
}